"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.default = void 0;

var _react = _interopRequireDefault(require("react"));

var _FixedSizeList = _interopRequireDefault(require("./FixedSizeList"));

var _components = require("@tarojs/components");

var _excluded = ["style", "onScroll", "onScrollNative", "layout"],
    _excluded2 = ["direction", "innerElementType", "itemElementType", "initialScrollOffset", "overscanCount"];

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }

function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }

function convertPxToInt(style) {
  if (typeof style === 'string') {
    var str = style.toLowerCase();

    if (/px$/.test(str)) {
      return Number(str.replace(/px$/, ''));
    }
  }

  return style;
}

var OuterScrollView = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var style = props.style,
      onScroll = props.onScroll,
      onScrollNative = props.onScrollNative,
      layout = props.layout,
      rest = _objectWithoutProperties(props, _excluded);

  var handleScroll = function handleScroll(event) {
    onScroll(_objectSpread(_objectSpread({}, event), {}, {
      currentTarget: _objectSpread(_objectSpread({}, event.detail), {}, {
        clientWidth: convertPxToInt(style.width),
        clientHeight: convertPxToInt(style.height)
      })
    }));

    if (typeof onScrollNative === 'function') {
      onScrollNative(event);
    }
  };

  return /*#__PURE__*/_react.default.createElement(_components.ScrollView, _objectSpread({
    ref: ref,
    style: style,
    scrollY: layout === 'vertical',
    scrollX: layout === 'horizontal',
    onScroll: handleScroll
  }, rest));
});

var VirtualList = /*#__PURE__*/_react.default.forwardRef(function (props, ref) {
  var _props$direction = props.direction,
      direction = _props$direction === void 0 ? 'ltr' : _props$direction,
      _props$innerElementTy = props.innerElementType,
      innerElementType = _props$innerElementTy === void 0 ? _components.View : _props$innerElementTy,
      _props$itemElementTyp = props.itemElementType,
      itemElementType = _props$itemElementTyp === void 0 ? _components.View : _props$itemElementTyp,
      _props$initialScrollO = props.initialScrollOffset,
      initialScrollOffset = _props$initialScrollO === void 0 ? 0 : _props$initialScrollO,
      _props$overscanCount = props.overscanCount,
      overscanCount = _props$overscanCount === void 0 ? 1 : _props$overscanCount,
      rest = _objectWithoutProperties(props, _excluded2);

  return /*#__PURE__*/_react.default.createElement(_FixedSizeList.default, _objectSpread(_objectSpread({
    ref: ref
  }, rest), {}, {
    itemElementType: itemElementType,
    innerElementType: innerElementType,
    outerElementType: OuterScrollView,
    direction: direction,
    initialScrollOffset: initialScrollOffset,
    overscanCount: overscanCount
  }));
});

var _default = VirtualList;
exports.default = _default;